package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.CourseRating;
import com.lanou.service.CourseRatingService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName CourseRatingController
 * @Description TODO
 * @Author cuiruitong
 * @Date 2025年08月13日 15:50
 * @Version 1.0
 */
@RestController
@RequestMapping("courseRating")
public class CourseRatingController {
    @Autowired
    private CourseRatingService courseRatingService;
    //增加
    @PostMapping
    public R addCourseRating(@RequestBody CourseRating courseRating) {
        courseRating.setTime(new Date());
        int i = courseRatingService.addCourseRating(courseRating);
        if (i>0){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteCourseRatingById(@PathVariable int id) {
        int i = courseRatingService.deleteCourseRatingById(id);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    //修改
    @PutMapping
    public R updateCourseRating(@RequestBody CourseRating courseRating) {
        int i = courseRatingService.updateCourseRatingByUserIdAndCourseId(courseRating);
        if (i>0){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    //查询并分页
    @GetMapping
    public R selectAllCourseRatingByCourseId(int courseId,@RequestParam(required = false,defaultValue = "1") int pageNum,@RequestParam(required = false,defaultValue = "10") int pageSize) {
        PageInfo<CourseRating> courseRatingPageInfo = courseRatingService.selectCourseRatingByCourseIdPage(courseId,pageNum,pageSize);
        return R.ok(courseRatingPageInfo);
    }

}
